import type { NavigationGuardNext, RouteLocationNormalized, RouteLocationNormalizedLoaded, Router } from 'vue-router'
import { userStore } from '@/stores/user'
const whiteList = ['/login', '/404']
export function permission(router: Router) {
  const store = userStore()
  router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalizedLoaded, next: NavigationGuardNext) => {
    if (whiteList.includes(to.path)) {
      next()
    } else if (!store.token) {
      next('/login')
    } else {
      const role = store.role
      if (role === 'manager') {
        next()
      } else {
        const mid = '/' + to.path.split('/')[1]
        if (rolePermission.common.includes(mid)) {
          next('/table')
        } else {
          next()
        }
      }
    }
  })
  router.afterEach((to, from) => {
    if (to.matched.length === 0) {
      router.push('/404')
    }
  })
}
export const rolePermission = {
  // 记录不能访问的路由
  common: ['/tecent', '/vue', '/component', '/simpleRouter', '/lodash', '/pinia', '/api', '/qiankun', '/postMessage', '/indexdb', '/filter']
}
export const canIshow = (path: string, role: string) => {
  if (role === 'manager') { return true }
  return !rolePermission.common.includes(path)
}
